/**
 * Copyright (c) 2017-2018, zengjintao (1913188966@qq.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jfast.framework.aop;

import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jfast.framework.kit.StrKit;
import com.jfast.framework.web.core.Action;
import com.jfast.framework.web.core.param.ParaBuilder;
import net.sf.cglib.proxy.MethodProxy;

public class Invocation {

	private Object target;
	private Method method;
	private Action action;
	private Object[] args;
	private Object controller;
	private String[] urlPara = null;
	private Object returnValue;
	private Interceptor[] inteceptors;
	private HttpServletRequest  request;
	private HttpServletResponse response;
	private int count = 0;
	
	
	public HttpServletRequest getRequest() {
		return request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}
	
	public String[] getUrlPara() {
		return urlPara;
	}
	
	public Object getReturnValue() {
		return returnValue;
	}

	public MethodProxy getMethodProxy() {
		return methodProxy;
	}

	private MethodProxy methodProxy;

	public Object[] getArgs() {
		return args;
	}

	public Invocation(Object target, Method method,Object[] args,MethodProxy methodProxy,Interceptor[] inteceptors) {
		this.target = target;
		this.method = method;
		this.args = args;
		this.methodProxy = methodProxy;
		this.inteceptors = inteceptors;
	}
	
	public Invocation(Action action,Object controller,Interceptor[] interceptors,
			HttpServletRequest request,HttpServletResponse response) {
		this.controller = controller;
		this.action = action;
		this.inteceptors = interceptors;
		this.request = request;
		this.response = response;
		if(StrKit.isNotEmpoty(this.action.getUrlPara())){
			this.urlPara = StrKit.spilt(this.action.getUrlPara(), "/");
		}
		this.args = ParaBuilder.getParaBuilder().builder(action,request,response).getPrams();
	}
	
	public Object getController() {
		return controller;
	}

	public Action getAction() {
		return action;
	}

	public Interceptor[] getInteceptors() {
		return inteceptors;
	}

	public Object getTarget() {
		return target;
	}

	public Method getMethod() {
		return method;
	}

	protected  void invoke(){
		
	}
	
	public void invoke(HttpServletRequest request,HttpServletResponse response){
		if(count<inteceptors.length){
			inteceptors[count++].inteceptor(this,request,response);
		}
		else if(count++ == inteceptors.length){
			if(action != null){
				try {
					Method method = action.getMethod();
					returnValue = method.invoke(controller,args);
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
		}
    }
}
